﻿/*
 * 版 本 v1.0
 * Copyright (c) 2013-2018 广州市同慧信息科技有限公司
 * 创建人：ftquan
 * 日 期：2017.04.17
 * 描 述：支出事项配置表单
 */
var refreshGirdData; // 更新数据
var selectedRow;
var bootstrap = function ($, learun) {
    "use strict";
    var payitemId = "";

    var page = {
        init: function () {
            page.initGrid_payitem();
            page.initGrid_form();
            page.bind();
        },
        bind: function () {
            // 搜索框初始化
            $('#txt_Keyword').on("keypress", function (e) {
                if (event.keyCode == "13") {
                    var $this = $(this);
                    var keyword = $this.val();
                    page.search_payitem({keyword: keyword});
                }
            });
            // 查询
            $('#btn_Search').on('click', function () {
                var keyword = $('#txt_Keyword').val();
                page.search_payitem({ keyword: keyword });
            });
            // 刷新
            $('#lr_refresh').on('click', function () {
                location.reload();
            });
            // 新增
            $('#lr_add').on('click', function () {
                selectedRow = null;
                if(!payitemId) {
                    learun.alert.warning('请选择支出事项！');
                    return;
                }
                learun.layerForm({
                    id: 'Form',
                    title: '添加子项',
                    url: config.webportal_server + 'pages/base/payitem/form/form.html?payitemId='+payitemId+'&access_token=' + config.getToken().access_token,
                    width: 650,
                    height: 680,
                    callBack: function (id) {
                        return top[id].acceptClick(refreshGirdData);
                    }
                });
            });
            // 编辑
            $('#lr_edit').on('click', function () {
                var keyValue = $('#gridtable_form').jfGridValue('id');
                selectedRow = $('#gridtable_form').jfGridGet('rowdata');
                if (learun.checkrow(keyValue)) {
                    learun.layerForm({
                        id: 'form',
                        title: '编辑子项',
                        url: config.webportal_server + 'pages/base/payitem/form/form.html?payitemId='+payitemId+'&access_token=' + config.getToken().access_token,
                        width: 650,
                        height: 680,
                        callBack: function (id) {
                            return top[id].acceptClick(refreshGirdData);
                        }
                    });
                }
            });
            // 删除
            $('#lr_delete').on('click', function () {
                var keyValue = $('#gridtable_form').jfGridValue('id');
                if (learun.checkrow(keyValue)) {
                    learun.layerConfirm('该动作执行后此项将彻底被删除，是否确认删除该项?', function (res) {
                        if (res) {
                            learun.deleteForm(config.base_server() + 'basepayitemforms/delete/' + keyValue + '?access_token=' + config.getToken().access_token, {keyValue: keyValue}, function () {
                                refreshGirdData();
                            });
                        }
                    });
                }
            });
        },
        initGrid_payitem: function () {
            $('#gridtable_payitem').jfGrid({
                url: config.base_server() + 'basepayitems/list?access_token=' + config.getToken().access_token,
                headData: [
                    { label: "编号", name: "code", width: 80, align: "left" },
                    { label: "名称", name: "name", width: 200, align: "left" },
                    {
                        label: "支出分类", name: "category", width: 120, align: "center",
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'PayItemCategorys',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                        }
                    }
                ],
                onSelectRow: function (row) {
                    selectedRow = $('#gridtable_payitem').jfGridGet('rowdata');
                    var keyValue = row.id;
                    payitemId = keyValue;
                    if (learun.checkrow(keyValue)) {
                       page.search_form({payitemId: keyValue});
                    }
                },
                isPage: true,
//                isTree: true,
                mainId: 'id',
                parentId: 'parentId'
            });
            page.search_payitem();
        },
        initGrid_form: function () {
            $('#gridtable_form').jfGrid({
                url: config.base_server() + 'basepayitemforms/findByPayitemId?access_token=' + config.getToken().access_token,
                headData: [
                    { label: "编号", name: "code", width: 60, align: "center" },
                    { label: "名称", name: "name", width: 150, align: "left" },
                    { label: "默认值", name: "defaultValue", width: 90, align: "center" },
                    { label: "最大值", name: "maxValue", width: 90, align: "center" },
                    {
                        label: "数据类型", name: "dataType", width: 90, align: "center",
                        formatterAsync: function (callback, value, row) {
                            learun.clientdata.getAsync('dataItem', {
                                key: value,
                                code: 'dataTypes',
                                callback: function (_data) {
                                    callback(_data.text);
                                }
                            });
                       }
                    },
                    {
                        label: "是否必填", name: "required", width: 90, align: "center",
                        formatter: function (cellvalue) {
                           return cellvalue == 1 ? "√" : "";
                        }
                    },
                    {
                        label: "是否只读", name: "readonly", width: 90, align: "center",
                        formatter: function (cellvalue) {
                           return cellvalue == 1 ? "√" : "";
                        }
                    },
                    { label: "申请公式", name: "applyFormula", width: 150, align: "left" },
                    { label: "报销公式", name: "bgtFormula", width: 150, align: "left" },
                    {
                        label: "是否总金额", name: "totalMoney", width: 90, align: "center",
                        formatter: function (cellvalue) {
                           return cellvalue == 1 ? "√" : "";
                        }
                    },
                    { label: "备注", name: "description", width: 200, align: "left" }
                ],
                isPage: false,
                mainId: 'id'
            });
        },
        search_payitem: function (param) {
            $('#gridtable_payitem').jfGridSet('reload', param);
        },
        search_form: function (param) {
            param.payitemId = payitemId;
            $('#gridtable_form').jfGridSet('reload', param);
        }
    };

    // 保存数据后回调刷新
    refreshGirdData = function (item, id) {
        if(!!item) {
            var postData = [];
            var bgtecoIds = item.value.split(',');

            for(var i=0;i<bgtecoIds.length;i++) {
                var item = {};
                item.bgtecoId = bgtecoIds[i];
                item.payitemId = payitemId;
                postData.push(item);
            }
            $.lrSaveForm(config.base_server() + 'basepayitemforms/saveOrUpdate?access_token=' + config.getToken().access_token, JSON.stringify(postData), function (res) {
                page.search_form({});
            });
        } else {
             page.search_form({});
        }
    }

    page.init();
}


